package com.wnxy.carl.carlorderassembly.controller;


import com.wnxy.carl.api.OrderApi;
import com.wnxy.carl.carlorderassembly.service.IOrderInfoService;
import com.wnxy.carl.carlorderassembly.service.IOrderService;
import com.wnxy.carl.dto.OrderDTO;
import com.wnxy.carl.dto.PageDTO;
import com.wnxy.carl.dto.StatisticsOrderDTO;
import com.wnxy.carl.param.PageParam;
import com.wnxy.carl.param.order.*;
import com.wnxy.carl.response.EPage;
import com.wnxy.carl.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-11-12
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController implements OrderApi {

    @Resource
    private IOrderService orderService;


    /**
     * 生成订单 OrderDTO
     * @param generateOrderParam
     * @return
     */
    @Override
    public Result<OrderDTO> generateOrder(GenerateOrderParam generateOrderParam) {
        log.info("生成订单功能：参数={}",generateOrderParam);
        OrderDTO orderDTO = orderService.generateOrder(generateOrderParam);

        return Result.success(OrderDTO.class,"生成完成",orderDTO);
    }

    /**
     * 修改订单 OrderDTO
     * @param amendOrderParam
     * @return
     */
    @Override
    public Result<OrderDTO> amendOrder(AmendOrderParam amendOrderParam) {
        log.info("修改订单功能：参数={}",amendOrderParam);
        OrderDTO orderDTO = orderService.amendOrder(amendOrderParam);

        return Result.success(OrderDTO.class,"修改成功",orderDTO);
    }

    /**
     * 支付宝支付 String
     * @param orderId
     * @return
     */
    @Override
    public Result alipay(Integer orderId) {
        return null;
    }

    /**
     * 账户余额支付 Boolean
     * @param moneyPayParam
     * @return
     */
    @Override
    public Result balancePay(MoneyPayParam moneyPayParam) {
        log.info("账户余额支付功能：参数={}",moneyPayParam);
        orderService.balancePay(moneyPayParam);
        return Result.success("付款完成");
    }

    /**
     * 用户查询订单 PageDTO<OrderDTO>
     * @param pageParam
     * @return
     */
    @Override
    public Result<? extends EPage<OrderDTO>> myOrder(PageParam<MyOrderParam> pageParam) {
        log.info("用户查询订单功能：参数={}",pageParam);
        PageDTO<OrderDTO> pageDTO = orderService.myOrder(pageParam);
        return Result.success(pageDTO);
    }

    /**
     * 门店查询订单 PageDTO<OrderDTO>
     * @param pageParam
     * @return
     */
    @Override
    public Result<? extends EPage<OrderDTO>> seeStoreOrder(PageParam<SeeStoreOrderParam> pageParam) {
        log.info("门店查询订单功能：参数={}",pageParam);
        PageDTO<OrderDTO> pageDTO = orderService.seeStoreOrder(pageParam);
        EPage<OrderDTO> dtoePageEPage = new EPage<>();
        dtoePageEPage.setPage(pageDTO.getPage());
        dtoePageEPage.setPages(pageDTO.getPages());
        dtoePageEPage.setPagesSize(pageDTO.getPagesSize());
        dtoePageEPage.setList(pageDTO.getList());
        return Result.success(dtoePageEPage);
    }

    /**
     * 用于时间段订单信息的统计 StatisticsOrderDTO
     * @param pageParam
     * @return
     */
    @Override
    public Result<StatisticsOrderDTO> statisticsOrder(PageParam<StatisticsOrderParam> pageParam) {
        log.info("用于时间段订单信息的统计功能：参数={}",pageParam);
        PageDTO<OrderDTO> pageDTO = orderService.statisticsOrder(pageParam);
        pageParam.setPageIndex(1);
        pageParam.setPageSize(-1);
        PageDTO<OrderDTO> pageDTO1 = orderService.statisticsOrder(pageParam);
        StatisticsOrderDTO statisticsOrderDTO = new StatisticsOrderDTO();
        statisticsOrderDTO.setTotalAmount(0.00);
        statisticsOrderDTO.setTotalNumber(0);
        statisticsOrderDTO.setUnpaidNumber(0);
        statisticsOrderDTO.setShippedNumber(0);
        statisticsOrderDTO.setCompletedNumber(0);
        statisticsOrderDTO.setOrderDTOS(pageDTO);//订单集合

        List<OrderDTO> list = pageDTO1.getList();
        for (OrderDTO orderDTO: list) {
            statisticsOrderDTO.setTotalAmount(statisticsOrderDTO.getTotalAmount()+orderDTO.getOrderTotal());//统计完后的总金额
            statisticsOrderDTO.setTotalNumber(statisticsOrderDTO.getTotalNumber()+orderDTO.getGoodsNumber());//统计完后的商品数量
            if (orderDTO.getOrderStatus() == 0) {
                statisticsOrderDTO.setUnpaidNumber(statisticsOrderDTO.getUnpaidNumber() + 1);//统计完后未支付订单数量
            }
            if (orderDTO.getReceivingStatus() == 1){
                statisticsOrderDTO.setShippedNumber(statisticsOrderDTO.getShippedNumber()+1);//统计完后已发货订单数量
            }
            if (orderDTO.getReceivingStatus() == 2 || orderDTO.getReceivingStatus() == 3 || orderDTO.getSaleStatus() == 3){
                statisticsOrderDTO.setCompletedNumber(statisticsOrderDTO.getCompletedNumber()+1);//统计完后已完成（收货或者服务完成）订单数量
            }

        }
        return Result.success(StatisticsOrderDTO.class,"统计完成",statisticsOrderDTO);
    }

}

